home *** CD-ROM | disk | FTP | other *** search
/ 5 Star Games: DOS Edition 2 / 5 Star Games - DOS Edition (1995)(Ready to Run).iso / dbc / db_file.c < prev    next >
C/C++ Source or Header  |  1992-03-04  |  6KB  |  294 lines

  1. /****************************************************************************/
  2. /*                         DATABOSS MODULE: DB_FILE.C                       */
  3. /****************************************************************************/
  4.  
  5. #include "db_lsc.h"
  6.  
  7. #include <ctype.h>
  8. #ifdef __TURBOC__
  9.     #include <dir.h>
  10. #else
  11.     #include <direct.h>
  12. #endif
  13. #include <dos.h>
  14. #include <errno.h>
  15. #include <io.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "db_types.h"
  20. #include "db_str.h"
  21. #include "db_file.h"
  22.  
  23. /***************************  INTERNAL VARIABLES  ***************************/
  24.  
  25. static string dospath = "PATH=";
  26. static uchar drivespec[4] = "?:\\";
  27.  
  28. /*****************************  IMPLEMENTATION  *****************************/
  29.  
  30. strptr getenvstr(strptr sout, string envar)
  31. {
  32.     strptr p;
  33.  
  34.     return (strcpy(sout,((p = getenv(envar)) == NULL) ? "\0" : p));
  35. }
  36.  
  37. strptr fsearchdir(pathstr sout, pathstr path)
  38. {
  39.     pathstr savepath;
  40.     int plen;
  41.  
  42.     if (path[0] != 0) {
  43.         getcurrentdir(savepath,0);
  44.         if (changedir(path)) {
  45.             getcurrentdir(sout,0);
  46.             plen = strlen(sout);
  47.             if (sout[plen-1] != '\\') {
  48.                 sout[plen++] = '\\';
  49.                 sout[plen] = '\0';
  50.             }
  51.         }
  52.         else
  53.             sout[0] = '\0';
  54.         changedir(savepath);
  55.     }
  56.     else
  57.     sout[0] = '\0';
  58.     return (sout);
  59. }
  60.  
  61. strptr fsearchpath(pathstr sout, pathstr path)
  62. {
  63.     pathstr tpath;
  64.     namestr name;
  65.     extstr ext;
  66.     findrec frec;
  67.     int plen;
  68.  
  69.     if (path[0] != '\0') {
  70.         plen = strlen(path);
  71.         if ((plen >= 2) && (path[1] == ':') && (path[2] != '\\')) {
  72.             strcopy(tpath,path,0,2);
  73.             strcat(fsearchdir(tpath,tpath),&path[2]);
  74.             plen = strlen(tpath);
  75.         }
  76.         else
  77.             strcpy(tpath,path);
  78.         if (tpath[plen-1] == '\\') {
  79.             if ((plen == 3) && (tpath[1] == ':'))
  80.                 strcpy(sout,tpath);
  81.             else
  82.                 fsearchdir(sout,tpath);
  83.         }
  84.         else {
  85.             if (db_findfirst(tpath,&frec,A_DIREC)) {
  86.                 if ((frec.attrib & A_DIREC) != 0)
  87.                     fsearchdir(sout,tpath);
  88.                 else {
  89.                     fsplit(tpath,sout,name,ext);
  90.                     strconcat(sout,fsearchdir(tpath,sout),name,ext,NULL);
  91.                 }
  92.             }
  93.             else
  94.                 sout[0] = '\0';
  95.         }
  96.     }
  97.     else
  98.     sout[0] = '\0';
  99.     return (strupr(sout));
  100. }
  101.  
  102. strptr fsearch(pathstr fnout, pathstr fnin, string path)
  103. {
  104.     string userpath;
  105.     namestr name;
  106.     extstr ext;
  107. #ifdef __TURBOC__
  108.     strptr p;
  109. #else
  110.     char pathbuf[256];
  111. #endif
  112.  
  113.     if (*fnin != '\0') {
  114.         fsplit(fnin,userpath,name,ext);
  115.         if (*userpath != '\0')
  116.             fsearchpath(fnout,fnin);
  117.         else {
  118.             getenvstr(&dospath[5],"PATH");
  119.             strcat(stpcpy(userpath,"PATH="),path);
  120. #ifdef __TURBOC__
  121.             if ((p = (putenv(userpath) == 0) ? searchpath(fnin) : NULL) != NULL)
  122.                 strcpy(fnout,p);
  123.             else
  124.                 *fnout = '\0';
  125. #else
  126.             if (putenv(userpath) == 0) {
  127.                 _searchenv(fnin,"PATH",pathbuf);
  128.         strcpy(fnout,pathbuf);
  129.       }
  130.             else
  131.                 *fnout = '\0';
  132. #endif
  133.             putenv(dospath);
  134.         }
  135.     }
  136.     else
  137.         *fnout = '\0';
  138.     return(fnout);
  139. }
  140.  
  141. void fsplit(pathstr path, dirstr dir, namestr name, extstr ext)
  142. {
  143.     drivestr drive;
  144.  
  145. #ifdef __TURBOC__
  146.     fnsplit(path,drive,dir,name,ext);
  147. #else
  148.     _splitpath(path,drive,dir,name,ext);
  149. #endif
  150.     strinsert(drive,dir,0);
  151. }
  152.  
  153. strptr fullspec(pathstr sout, pathstr path)
  154. {
  155.     union REGS regs;
  156.     pathstr cdir;
  157.  
  158.     strcpy(sout,path);
  159.     if ((strlen(path) < 2) || (path[1] != ':')) {
  160.         regs.h.ah = 0x19;
  161.         intdos(®s,®s);
  162.         strinsert("?:",sout,0);
  163.         sout[0] = (char)('A'+regs.h.al);
  164.     }
  165.     if (sout[2] != '\\') {
  166.         getcurrentdir(cdir,(byte)(toupper(sout[0])-'A'+1));
  167.         if (cdir[strlen(cdir)-1] != '\\')
  168.             strchcat(cdir,'\\');
  169.             strinsert(&cdir[2],sout,2);
  170.     }
  171.     return (sout);
  172. }
  173.  
  174. strptr fexpand(pathstr sout, pathstr path)
  175. {
  176.     pathstr tpath;
  177.     strptr ip,op;
  178.     int dots;
  179.  
  180.     if (path[0] != '\0') {
  181.         fullspec(tpath,path);
  182.         ip = tpath;
  183.         op = sout;
  184.         do {
  185.             while ((*ip != '\\') && (*ip != '\0')) *op++ = *ip++;
  186.             if (*ip == '\\') {
  187.                 if (*(op-1) != '\\') *op++ = '\\';
  188.                 ip++;
  189.                 dots = 0;
  190.                 while (*ip == '.') {
  191.                     dots++;
  192.                     ip++;
  193.                 }
  194.                 if (dots-- > 0) {
  195.                     while ((*ip != '\\') && (*ip != '\0')) ip++;
  196.                     while (dots > 0) {
  197.                         if (*(op-2) != ':') {
  198.                             do op--; while (*(op-1) != '\\');
  199.                         }
  200.                         dots--;
  201.                     }
  202.                 }
  203.             }
  204.         } while (*ip != '\0');
  205.         *op = '\0';
  206.     }
  207.     else
  208.         getcurrentdir(sout,0);
  209.     strupr(sout);
  210.     return (sout);
  211. }
  212.  
  213. bool getcurrentdir(pathstr sout, byte drive)
  214. {
  215.     union REGS regs;
  216.     struct SREGS sregs;
  217.  
  218.     if (drive == 0) {
  219.         regs.h.ah = 0x19;
  220.         intdos(®s,®s);
  221.         drivespec[0] = (uchar) (regs.h.al+'A');
  222.     }
  223.     else
  224.         drivespec[0] = (uchar) (drive+'A'-1);
  225.     strcpy(sout,drivespec);
  226.     regs.h.ah = 0x47;
  227.     regs.h.dl = drive;
  228.     regs.x.si = FP_OFF(sout)+3;
  229.     sregs.ds = FP_SEG(sout);
  230.     intdosx(®s,®s,&sregs);
  231.     if (regs.x.cflag) sout[3] = '\0';
  232.     return ((bool) (regs.x.cflag == 0));
  233. }
  234.  
  235. bool changedrive(uchar drive)
  236. {
  237.     union REGS regs;
  238.     byte dosdrive;
  239.  
  240.     dosdrive = toupper(drive) - 'A';
  241.     regs.h.ah = 0x0E;
  242.     regs.h.dl = dosdrive;
  243.     intdos(®s,®s);
  244.     regs.h.ah = 0x19;
  245.     intdos(®s,®s);
  246.     return ((bool) (regs.h.al == dosdrive));
  247. }
  248.  
  249. bool changedir(pathstr path)
  250. {
  251.     bool changeok;
  252.     pathstr tpath;
  253.     int plen;
  254.  
  255.     if (path[0] != '\0') {
  256.         strcpy(tpath,path);
  257.         plen = strlen(tpath);
  258.         if ((plen > 1) && (tpath[plen-1] == '\\') &&
  259.                 !((plen == 3) && (tpath[1] == ':')))
  260.             tpath[--plen] = '\0';
  261.         changeok = (tpath[1] == ':') ? changedrive(tpath[0]) : True;
  262.         if (changeok && !((plen == 2) && (tpath[1] == ':')))
  263.             changeok = (bool) (chdir(tpath) == 0);
  264.     }
  265.     else
  266.         changeok = False;
  267.     return (changeok);
  268. }
  269.  
  270. bool db_eof(FILE *f)
  271. {
  272.     return ((bool) (ftell(f) >= filelength(fileno(f))));
  273. }
  274.  
  275. bool db_findfirst(pathstr path, findrec *frec, int attr)
  276. {
  277. #ifdef __TURBOC__
  278.     return ((bool) (findfirst(path,(struct ffblk *) frec,attr) == 0));
  279. #else
  280.     return ((bool) (_dos_findfirst(path,attr,(struct find_t *) frec) == 0));
  281. #endif
  282. }
  283.  
  284. bool db_findnext(findrec *frec)
  285. {
  286. #ifdef __TURBOC__
  287.     return ((bool) (findnext((struct ffblk *) frec) == 0));
  288. #else
  289.     return ((bool) (_dos_findnext((struct find_t *) frec) == 0));
  290. #endif
  291. }
  292.  
  293. /*****************************  END OF DB_FILE.C  ***************************/
  294.